package com.n.Internet.Case;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.Properties;

/**
 * bug:注册之后不能继续登录
 * 注册之后服务器停止运行与客户端断开连接
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-27-20:12 20:12
 */
public class Server {
    private static Properties properties=new Properties();

    public static void main(String[] args) {
        BufferedReader response=null;           //响应数据
        PrintWriter request=null;               //向客户端发送数据
        boolean flag=true;
        try {
                ServerSocket serverSocket=new ServerSocket(8888);
                Socket socket = serverSocket.accept();
                //响应客户机的请求
                response= new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                switch (Integer.parseInt(response.readLine())){
                    case 1:
                        String loginDate=response.readLine();
                        System.out.println("收到登录");
                        //将登录验证信息返回给客户机
                        request= new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                        request.println(loginVerify(loginDate));
                        request.flush();
                        break;
                    case 2:
                        String information=response.readLine();
                        //将响应的数据保存到文件中并返回响应的结果
                        request= new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
                        request.println(saveFile(information));
                        request.flush();
                        break;
                }


        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //request.close();
                response.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 登录信息的验证
     * loginDate:  1001:123123
     * */
    public static String loginVerify(String loginDate){
        //从服务器文件中进行读取数据
        BufferedReader bufferedReader=null;
        try {
             bufferedReader= new BufferedReader(new InputStreamReader(new FileInputStream("src\\Server file\\information.properties")));
            properties.load(bufferedReader);
            for (Map.Entry entry:
                    properties.entrySet()) {
                //判断账号是否在
                if (entry.getKey().equals(loginDate.substring(0,4))){
                    System.out.println("账号存在");
                    //进行pwd密码内容的截取，将object转换成string
                    Integer head=entry.getValue().toString().indexOf("pwd")+4;
                    Integer end=entry.getValue().toString().indexOf("age")-1;
                    String pwd=entry.getValue().toString().substring(head,end);
                    //进行密码的判断
                    if (pwd.equals(loginDate.substring(loginDate.indexOf(":")+1))){
                        return "登录成功！";
                    }else{
                        return "密码错误";
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "账号不存在！";
    }
    /**
     * 将服务器中数据保存至文件 Server file\information.properties
     * 将接受的信息进行整理
     * 将信息中的id进行分离，为保存在properties文件中做准备
     *
     * */
    public static String saveFile(String information){
        PrintWriter printWriter=null;       //输入到服务器文件
        try {

            //创建输入文件的流
            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream("src\\Server file\\information.properties", true), "UTF-8"));
            //解析数据中id的值
            String id=tidy(information);
            //保存到文件中

            properties.setProperty(id,information);//解析的id作为”=”前的key,information作为“=”后的value
            properties.store(printWriter,"");//store()将流中的数据写入到properties文件中
            System.out.println("保存成功");
            printWriter.close();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }
        return "保存成功";
    }
    /**
     * substring(首，尾)字符串的按照位置分割
     * indexOf('字符')查找字符的位置
     * id{id:1001,name:tom,pwd:123,age:20,score:98.5}
     * @return id信息
     * */

    public static String tidy(String information){
        //分割id
        return information.substring(information.indexOf(":")+1,information.indexOf(","));
    }
}
